local kuanggu = fk.CreateSkill{
  name = "kl__kuanggu",
}
kuanggu:addEffect(fk.Damage, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(kuanggu.name) and target == player and (data.extra_data or {}).kuanggucheck
  end,
  on_trigger = function(self, event, target, player, data)
    for _ = 1, data.damage do
      if event:isCancelCost(self) or not player:hasSkill(kuanggu.name) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local choices = {"draw1"}
    if player:isWounded() then
      table.insert(choices,"recover")
      table.insert(choices,"kl__kuanggu_beishui")
    end
    table.insert(choices,"Cancel")
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = kuanggu.name,
    })
    if choice ~= "Cancel" then
      event:setCostData(self, {choice = choice})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    if choice == "recover" or choice == "kl__kuanggu_beishui" then
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = kuanggu.name,
      })
    end
    if choice == "draw1" or choice == "kl__kuanggu_beishui" then
      player:drawCards(1, kuanggu.name)
    end
    if choice == "kl__kuanggu_beishui" then
      if player:isNude() then return end
      room:askToDiscard(player,{
        min_num = 1,
        max_num = 1,
        skill_name = kuanggu.name,
        include_equip = true,
      })
    end
  end,
})
kuanggu:addEffect(fk.BeforeHpChanged, {
  can_refresh = function(self, event, target, player, data)
    return data.damageEvent and player == data.damageEvent.from and player:compareDistance(target, 2, "<")
  end,
  on_refresh = function(self, event, target, player, data)
    data.damageEvent.extra_data = data.damageEvent.extra_data or {}
    data.damageEvent.extra_data.kuanggucheck = true
  end,
})


return kuanggu
